﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;

namespace PhoneApp1
{
    public partial class Page1 : PhoneApplicationPage
    {
        Algorithem algo;

        private AppSettings settings = new AppSettings();

        public Page1()
        {
            InitializeComponent();
            algo = Algorithem.GetInstance();

            pointsEditBox.Text = settings.GetValueOrDefault<double>(AppSettings.pointsSettingKeyName, AppSettings.pointsSettingDefault).ToString();
            addingsWithNoPension.Text = settings.GetValueOrDefault<double>(AppSettings.salaryWithoutPensionSettingKeyName, AppSettings.salaryWithoutPensionSettingDefault).ToString();
            pensionPercent.Text = settings.GetValueOrDefault<double>(AppSettings.pensionPercentageSettingKeyName, AppSettings.pensionPercentageSettingDefault).ToString();
            drivingRefundTextBox.Text = settings.GetValueOrDefault<double>(AppSettings.drivingRefundSettingKeyName, AppSettings.drivingRefundSettingDefault).ToString();
            kerenHishtTextBox.Text = settings.GetValueOrDefault<double>(AppSettings.kerenHishtalmutPercentSettingKeyName, AppSettings.kerenHishtalmutPercentSettingDefault).ToString();
            isLeasingCheckBox.IsChecked = settings.GetValueOrDefault<bool>(AppSettings.isLeasingSettingKeyName, AppSettings.isLeasingSettingDefault);
            isHybridCheckBox.IsChecked = settings.GetValueOrDefault<bool>(AppSettings.isHybridSettingKeyName, AppSettings.isHybridSettingDefault);
            companyPaymentTextBox.Text = settings.GetValueOrDefault<double>(AppSettings.companyPaymentForCarKeyName, AppSettings.companyPaymentForLeasingSettingDefault).ToString();
            carsPriceTextBox.Text = settings.GetValueOrDefault<double>(AppSettings.carPriceSettingKeyName, AppSettings.carsPriceSettingDefault).ToString();
            if (isLeasingCheckBox.IsChecked == false)
            {
                setLeasingEnabled(false);
            }
        }

        private void TextBox_GotFocus_1(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            textBox.Background = new SolidColorBrush(Colors.Black);
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            saveSettingTextBox(AppSettings.pointsSettingKeyName, AppSettings.pointsSettingDefault, pointsEditBox, "הגדר מחדש מספר נקודות זיכוי");

            saveSettingTextBox(AppSettings.salaryWithoutPensionSettingKeyName, AppSettings.salaryWithoutPensionSettingDefault, addingsWithNoPension,"הגדר מחדש תוספות ללא הפרשות לפנסיה");
            saveSettingTextBox(AppSettings.pensionPercentageSettingKeyName, AppSettings.pensionPercentageSettingDefault,pensionPercent,"אחוז לפנסיה דפוק");
            saveSettingTextBox(AppSettings.drivingRefundSettingKeyName, AppSettings.drivingRefundSettingDefault,drivingRefundTextBox,"הגדר מחדש החזרי נסיעות");
            saveSettingTextBox(AppSettings.kerenHishtalmutPercentSettingKeyName, AppSettings.kerenHishtalmutPercentSettingDefault,kerenHishtTextBox,"הגדר מחדש אחוז לקרן השתלמות");
            saveSettingTextBox(AppSettings.companyPaymentForCarKeyName, AppSettings.companyPaymentForLeasingSettingDefault, companyPaymentTextBox, "הגדר מחדש תשלום לחברה עבור ליסינג");
            settings.AddOrUpdateValue(AppSettings.isLeasingSettingKeyName, isLeasingCheckBox.IsChecked??false);
            settings.AddOrUpdateValue(AppSettings.isHybridSettingKeyName, isHybridCheckBox.IsChecked ?? false);
            settings.Save();
        }

        

        private void saveSettingTextBox(string name, double defaultValue, TextBox textBox, string message)
        {
            double value = defaultValue;
            try
            {
                value = Double.Parse(textBox.Text);
                settings.AddOrUpdateValue(name, value);
            }
            catch (Exception)
            {
                MessageBox.Show(message, "מחשבון הנטו", MessageBoxButton.OK);
            }
        }

        private void isLeasingCheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            bool valueOfCheckBox = isLeasingCheckBox.IsChecked ?? false;
            setLeasingEnabled(valueOfCheckBox);
            settings.AddOrUpdateValue(AppSettings.isLeasingSettingKeyName, valueOfCheckBox);
        }

        private void setLeasingEnabled(bool isEnable)
        {
            isHybridCheckBox.IsEnabled = isEnable;
            companyPaymentTextBox.IsEnabled = isEnable;
            carsPriceTextBox.IsEnabled = isEnable;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBoxResult resualt = MessageBox.Show("אתה בטוח שאתה רוצה לשחזר את ההגדרות?", "שחזור הגדרות יצרן", MessageBoxButton.OKCancel);
            if (resualt == MessageBoxResult.OK)
            {
                pointsEditBox.Text = AppSettings.pointsSettingDefault.ToString();
                addingsWithNoPension.Text = AppSettings.salaryWithoutPensionSettingDefault.ToString();
                pensionPercent.Text = AppSettings.pensionPercentageSettingDefault.ToString();
                drivingRefundTextBox.Text = AppSettings.drivingRefundSettingDefault.ToString();
                kerenHishtTextBox.Text = AppSettings.kerenHishtalmutPercentSettingDefault.ToString();
                isLeasingCheckBox.IsChecked = AppSettings.isLeasingSettingDefault;
                isHybridCheckBox.IsChecked = AppSettings.isHybridSettingDefault;
                companyPaymentTextBox.Text = AppSettings.companyPaymentForLeasingSettingDefault.ToString();
                
                settings.AddOrUpdateValue(AppSettings.pointsSettingKeyName, AppSettings.pointsSettingDefault);
                settings.AddOrUpdateValue(AppSettings.salaryWithoutPensionSettingKeyName, AppSettings.salaryWithoutPensionSettingDefault);
                settings.AddOrUpdateValue(AppSettings.pensionPercentageSettingKeyName, AppSettings.pensionPercentageSettingDefault);
                settings.AddOrUpdateValue(AppSettings.drivingRefundSettingKeyName, AppSettings.drivingRefundSettingDefault);
                settings.AddOrUpdateValue(AppSettings.kerenHishtalmutPercentSettingKeyName, AppSettings.kerenHishtalmutPercentSettingDefault);
                settings.AddOrUpdateValue(AppSettings.isLeasingSettingKeyName, AppSettings.isLeasingSettingDefault);
                settings.AddOrUpdateValue(AppSettings.isHybridSettingKeyName, AppSettings.isHybridSettingDefault);
                settings.AddOrUpdateValue(AppSettings.companyPaymentForCarKeyName, AppSettings.companyPaymentForLeasingSettingDefault);
            }

        }
       
    }
}